﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
namespace Odyssey.Consul
{
    using Http;
    using Implementations;

    public static class ConsulExtension
    {
        public static IServiceCollection AddConsul(this IServiceCollection serviceCollection, Action<IConsulConfiguration> actionBuildConsulConfiguration)
        {
            return serviceCollection.AddOdysseyHttp((httpConfiguration) =>
             {
                 httpConfiguration.JsonSerializerSettingsBuilder = () =>
                 {
                     return new JsonSerializerSettings()
                     {
                         NullValueHandling = NullValueHandling.Ignore,
                         DefaultValueHandling = DefaultValueHandling.Ignore,
                     };
                 };

             }).AddConsulCore(actionBuildConsulConfiguration);
        }

        public static IServiceCollection AddConsulCore(this IServiceCollection serviceCollection, Action<IConsulConfiguration> actionBuildConsulConfiguration)
        {
            var consulConfiguration = new ConsulConfiguration();
            actionBuildConsulConfiguration.Invoke(consulConfiguration);
            serviceCollection.AddSingleton<IConsulConfiguration>(consulConfiguration);
            serviceCollection.AddSingleton<IACLs, ACLs>();
            serviceCollection.AddSingleton<IAgent, Agent>();
            serviceCollection.AddSingleton<ICatalog, Catalog>();
            serviceCollection.AddSingleton<IConfig, Config>();
            serviceCollection.AddSingleton<IConnect, Connect>();
            serviceCollection.AddSingleton<ICoordinate, Coordinate>();
            serviceCollection.AddSingleton<IHealth, Health>();
            serviceCollection.AddSingleton<IKVStore, KVStore>();
            serviceCollection.AddSingleton<ISession, Session>();
            serviceCollection.AddSingleton<ConsulDispatch>();
            return serviceCollection;
        }
    }
}
